<!DOCTYPE html>
{extend name="common/application" /}
{block name="title"}<title>个人中心-地址管理</title>{/block}
{block name="css"}
<link rel="stylesheet" type="text/css" href="_CSS_/help.css"/>
<link rel="stylesheet" type="text/css" href="_CSS_/address.css"/>

<style type="text/css">
    .hide {
        display: none !important;
    }
	
	.defaultBton{
		    display: none;
			width: 76px;
			height: 26px;
			line-height: 26px;
			background: #999;
			color: #FFFFFF;
			
	}
</style>
{/block}
<!-- 主体内容 -->
{block name="content"}

<!-- 主体内容 -->
<div class="main">
    <div class="mainBody normalBox clearfix">
        <!--左菜单-->
        <ul class="menuList fl">
            <li>
                <a class="menuItem dropBg" href="personal.html"><span class="icons icon-account"></span>账户管理</a>
                <div class="childMenu">
                    <a class="menuchildItem" href="personal.html">个人资料</a>
                    <a class="menuchildItem" href="certification.html">实名认证</a>
                    <a class="menuchildItem" href="account.html">账户安全</a>
                </div>
            </li>
            <li>
                <a class="menuItem dropBg" href="order.html"><span class="icons icon-order"></span>我的订单</a>
                <div class="childMenu">
                    <a class="menuchildItem" href="order.html">我的订单</a>
                </div>
            </li>
            <li>
                <a class="menuItem dropBg" href="collect_news.html"><span class="icons icon-collection"></span>我的收藏</a>
                <div class="childMenu">
                    <a class="menuchildItem" href="collect_news.html">资讯收藏</a>
                    <a class="menuchildItem" href="collect_course.html">课程收藏</a>
                    <a class="menuchildItem" href="collect_products.html">商品收藏</a>
                    <a class="menuchildItem" href="collect_stores.html">店铺收藏</a>
                </div>
            </li>
            <li class="showChild">
                <a class="menuItem dropBg" href="address.html"><span class="icons icon-address"></span>地址管理</a>
                <div class="childMenu">
                    <a class="menuchildItem activeMenu" href="address.html">地址管理</a>
                </div>
            </li>
            <li>
                <a class="menuItem dropBg" href="judge.html"><span class="icons icon-evaluate"></span>评价管理</a>
                <div class="childMenu">
                    <a class="menuchildItem" href="judge.html">评价管理</a>
                </div>
            </li>
            <li>
                <a class="menuItem dropBg" href="complain.html"><span class="icons icon-price"></span>申述退款</a>
                <div class="childMenu">
                    <a class="menuchildItem" href="complain.html">申述</a>
                    <a class="menuchildItem" href="refund_money.html">退款</a>
                </div>
            </li>
            <li class="bb0">
                <a class="menuItem dropBg" href="message.html"><span class="icons icon-news"></span>消息中心</a>
                <div class="childMenu">
                    <a class="menuchildItem" href="message.html">消息中心</a>
                </div>
            </li>
        </ul>
        <!--右内容-->
        <div class="menuContent fr">
            <div class="menuConHeader">
                <!--面包屑导航-->
                <div class="breadcrumb">
                    <p>当前位置：
                        <a href="address.html">发票与地址</a> &gt;
                        <a class="curPage" href="javascript:;">地址管理</a>
                    </p>
                </div>
            </div>
            <div class="menuConBody">
                <!--地址添加-->
                <form id="form1" action="" method="post" class='forms'>
                    <h5>新增收货地址</h5>
                    <div class='form-group'>
                        <label>收货人：</label>
                        <input name="nick" class='form-control people' type="text" placeholder="请输入你的姓名"/>
                    </div>
                    <div class='form-group'>
                        <label>电话号码：</label>
                        <input name="phone" class='form-control phone' type="text" placeholder="请输入你的电话号码"/>
                    </div>
                    <div class='form-group'>
                        <label>所在地区：</label>
                        <span id="distpicker1">
										<select id="provice"></select>
										<select id="city"></select>
										<select id="district"></select>
									</span>
                    </div>
                    <div class='form-group'>
                        <label>详细地址：</label>
                        <input name="address" class='form-control Address' type="text" placeholder="请输入详细地址"/>
                    </div>
                    <div class='form-group'>
                        <label>邮政编码：</label>
                        <input name="code" class='form-control code' type='text' placeholder="请输入当地邮政编码"/>
                        <a class="setDefault" href="javascript:;"><input type="checkbox" name="box"/>设置为默认地址</a>
                    </div>
                    <input type="submit" class="submitBtn" onclick="return false;"/>
                </form>
                <p class="tips"></p>
                <table class="tableList" border="" cellspacing="" cellpadding="">
                    <thead>
                    <tr>
                        <th class="w80">收货人</th>
                        <th class="w170">所在地区</th>
                        <th class="w240">详细地址</th>
                        <th class="w80">邮政编码</th>
                        <th class="w130">手机号码</th>
                        <th class="w80">操作</th>
                        <th class="w100"></th>
                    </tr>
                    </thead>
                    <tbody>

                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
{/block}


{block name="js"}

<!--<script src="_P_/laydata/laydate.js"></script>-->
<script src="_P_/layer/layer.js"></script>
<script src="_JS_/distpicker.data.js"></script>
<script src="_JS_/distpicker.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#distpicker1").distpicker(
            {
                placeholder: false,
                province: "湖北省",
                city: "武汉市",
                district: "洪山区"
            }
        );
        address_list();


        $('.submitBtn').on('click', function () {
            var userinfo = $.cookie('userinfo');
            if (userinfo) {
                var user_login = $.base64.decode(userinfo).split(",");
                var uid = user_login[0];
                var provice = $('#provice').val();
                var city = $('#city').val();
                var district = $('#district').val();
                var location = provice + '-' + city + '-' + district;
                var nick = $('.people').val();
                var phone = $('.phone').val();
                var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
                if (!myreg.test(phone)) {
                    layer.msg('手机号码格式不正确！', {icon: 5});
                    return false;
                }
                var code = $('.code').val();
                var address = $('.Address').val();
                var is_default;
                if ($('input[type="checkbox"]').is(':checked')) {
                    is_default = 1;
                } else {
                    is_default = 2;
                }
                $.ajax({
                    type: "get",
                    url: "/index.php/api/form/add_address",
                    data: {
                        'nick': nick,
                        'address': address,
                        'location': location,
                        'code': code,
                        'is_default': is_default,
                        'uid': uid,
                        'phone': phone,
                        'static': 1
                    },
                    dataType: 'jsonp',
					beforeSend: function () {
					var index = layer.load(1, {
						shade: [0.1, '#333333'] //0.1透明度的白色背景
					});
					},
                    success: function (msg) {
                        layer.closeAll();
                        if (msg.code == 200) {
                            address_list();
                            $("#distpicker1").distpicker(
                                {
                                    placeholder: false,
                                    province: "湖北省",
                                    city: "武汉市",
                                    district: "洪山区"
                                }
                            );
                            $('.people').val('');
                            $('.phone').val('');
                            $('.code').val('');
                            $('.Address').val('');
                            layer.msg('添加成功！', {icon: 1});
                        }
                        if (msg.code == 1000) {
                            layer.msg(msg.message, {icon: 5});
                        }
                    }
                });

            } else {
                layer.msg('还未登录！', {icon: 51});
            }
        })
    });

    function address_list() {
        var userinfo = $.cookie('userinfo');
        if (userinfo) {
            var user_login = $.base64.decode(userinfo).split(",");
            var uid = user_login[0];
            $.ajax({
                type: "get",
                url: "/index.php/api/form/address_list",
                data: {
                    'uid': uid
                },
                dataType: 'jsonp',
                success: function (msg) {
                    if (msg.code == 200) {
                        if (msg.data == null) {
                            $('tbody').html("");
                        } else {
                            var len = msg.data.length;
                            var html = '';
                            for (var i = 0; i < msg.data.length; i++) {
								var name = cut(msg.data[i].nick,4);
								var areas = cut((msg.data[i].location.replace(/-/g, " ")),20);
								var address = cut(msg.data[i].address,30);
                                html += '<tr class="items" num="' + msg.data[i].id + '"><td title="'+ msg.data[i].nick +'">' + name + '</td>';
                                html += '<td style="text-align:left;" title="'+ msg.data[i].location.replace(/-/g, " ") +'">' + areas + '</td><td style="text-align:left;" title="'+ msg.data[i].address +'">' + address + '</td>';
                                html += '<td>' + msg.data[i].code + '</td><td>' + msg.data[i].phone + '</td>';
                                html += '<td><a class="editBtn" href="javascript:;">修改</a><a class="delBtn" href="javascript:;">删除</a></td>';
                                if (msg.data[i].is_default == 1) {
                                    html += '<td><a class="defaultBtn hide" href="javascript:;">设为默认</a>';
                                    html += '<a class="staticBtn show" href="javascript:;">默认地址</a>';
                                } else {
                                    html += '<td><a class="defaultBtn" href="javascript:;">设为默认</a>';
                                    html += '<a class="staticBtn" href="javascript:;">默认地址</a>';
                                }

                                html += '</td></tr>';
                            }
                            $('.tips').html('已保存' + len + '条地址信息，还能保存' + (6 - parseInt(len)) + '条地址信息');
                            $('tbody').html(html);
                        }

                    } else if (msg.code == 1000) {
                        $('tbody').html("");
                    }
                    $('tbody .items').hover(function () {
                        $(this).find('.defaultBtn').addClass('show');
                    }, function () {
                        $(this).find('.defaultBtn').removeClass('show');
                    });
                    
                    /*地址修改*/
                    $('.editBtn').on('click', function () {
                        var address_id = $(this).parents('.items').attr('num');
                        layer.open({
                            type: 2,
                            title: '地址修改',
                            fix: false,
                            maxmin: true,
                            shadeClose: true,
                            scrollbar: false,
                            area: ['560px', '400px'],
                            content: 'edit_address.html?id=' + address_id,
                            btn: ['发送', '取消'],
                            yes: function (index, layero) {
                                var body = layer.getChildFrame('body', index);
                                // var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                                //得到iframe页的body内容
                                var is_default;
                                if (body.find('input[type="checkbox"]').is(':checked')) {
                                    is_default = 1;
                                } else {
                                    is_default = 2;
                                }
                                var phone = body.find('.phone').val();
                                var nick = body.find('.people').val();
                                var address = body.find('.Address').val();
                                var provice = body.find('#provice').val();
                                var city = body.find('#city').val();
                                var district = body.find('#district').val();
                                var location = provice + '-' + city + '-' + district;
                                var code = body.find('.code').val();
                                if (nick != '' && address != '' && location != '' && code != '' && phone != '') {
                                    layer.close(index);//如果设定了yes回调，需进行手工关闭
                                    $.ajax({
                                        type: "get",
                                        url: "/index.php/api/form/edit_address",
                                        data: {
                                            'id': address_id,
                                            'nick': nick,
                                            'address': address,
                                            'location': location,
                                            'code': code,
                                            'is_default': is_default,
                                            'uid': uid,
                                            'phone': phone,
                                            'static': 1
                                        },
                                        dataType: 'jsonp',
										beforeSend: function () {
										var index = layer.load(1, {
											shade: [0.1, '#333333'] //0.1透明度的白色背景
										});
										},
                                        success: function (msg) {
                                            layer.closeAll();
                                            if (msg.code == 200) {
                                                layer.msg('修改成功！', {icon: 1}, function () {
                                                    address_list();
                                                });
                                            }
                                            if (msg.code == 1000) {
                                                layer.msg('修改失败！', {icon: 5});
                                            }
                                        }
                                    });
                                } else {
                                    layer.msg('不能为空！', {icon: 5});
                                }
                            }
                        });
                    });
                    /*删除地址*/
                    $('.delBtn').on('click', function () {
                        var address_id = $(this).parents('.items').attr('num');
                        layer.open({
                            type: 1,
                            title: '删除地址',
                            closeBtn: false,
                            shadeClose: true,
                            scrollbar: false,
                            area: ['560px', '150px'],
                            content: "<p style='margin:20px 0px;font-size:14px;padding:0 20px;'>你确定要删除该条地址信息吗？</p>",
                            btn: ['确认', '等一等'],
                            yes: function (index, layero) {
                                //do something
                                layer.close(index); //如果设定了yes回调，需进行手工关闭
                                $.ajax({
                                    type: "get",
                                    url: "/index.php/api/form/address_del",
                                    data: {
                                        'id': address_id,
                                        'is_default': 3
                                    },
                                    dataType: 'jsonp',
									beforeSend: function () {
									var index = layer.load(1, {
										shade: [0.1, '#333333'] //0.1透明度的白色背景
									});
									},
                                    success: function (msg) {
										layer.closeAll();
                                        address_list();
                                        // layer.close(index);//如果设定了yes回调，需进行手工关闭
                                        if (msg.code == 200) {
                                            layer.msg('删除成功！', {icon: 1});
                                        }
                                        if (msg.code == 1000) {
                                            layer.msg('删除失败！', {icon: 5});
                                        }
                                    }
                                });
                            }
                        });
                    });
                }
            });
        } else {
            layer.msg('还未登录！', {icon: 51});
        }
    }
	/*设置默认*/
	$('tbody').on('click', '.defaultBtn', function () {
		var userinfo = $.cookie('userinfo');
        if (userinfo) {
            var user_login = $.base64.decode(userinfo).split(",");
            var uid = user_login[0];
			var address_id = $(this).parents('.items').attr('num');
			$.ajax({
				type: "get",
				url: "/index.php/api/form/address_del",
				data: {
					'id': address_id,
					'uid': uid,
					'is_default': 1
				},
				dataType: 'jsonp',
				beforeSend: function () {
				var index = layer.load(1, {
					shade: [0.1, '#333333'] //0.1透明度的白色背景
				});
				},
				success: function (msg) {
					layer.closeAll();
					address_list();
					if (msg.code == 200) {
						layer.msg('设置成功', {icon: 1});
					}else if (msg.code == 1000) {
						layer.msg('设置失败', {icon: 5});
					}
				}
			});
		} else {
            layer.msg('还未登录！', {icon: 51});
        }
		// $('.staticBtn').removeClass('show');
		// $(this).next().addClass('show');
		// $(this).removeClass('show');
		// /*默认地址置于最上部*/
		// var html = $(this).parents('.items').prop("outerHTML");
		// $('.items:first-child').before(html);
		// $(this).parents('.items').remove();
		// /*动态追加的元素，绑定悬浮事件*/
		// $('tbody .items').hover(function () {
		//     if ($(this).find('.staticBtn').hasClass('show')) {
		//         return false;
		//     } else {
		//         $(this).find('.defaultBtn').addClass('show');
		//     }
		// }, function () {
		//     $(this).find('.defaultBtn').removeClass('show');
		// });
	});
</script>
{/block}
			